package com.dts.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Enumeration;

@WebServlet(name = "info", urlPatterns = "/info")
public class ClientInfoServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String data = "中国";
        // 获取OutputStream输出流
        OutputStream outputStream = response.getOutputStream();
        // 通过设置响应头控制浏览器以UTF-8的编码显示数据，如果不加这句话，那么浏览器显示的将是乱码
        response.setHeader("content-type", "text/html;charset=UTF-8");
        // 将字符转换成字节数组，指定以UTF-8编码进行转换
        byte[] array = data.getBytes("UTF-8");
        // 使用OutputStream流向客户端输出字节数组
        outputStream.write(array);
    }

    public void doGet2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");// 设置将字符以"UTF-8"编码输出到客户端浏览器
        // 通过设置响应头控制浏览器以UTF-8的编码显示数据
        response.setHeader("content-type", "text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Enumeration<String> reqHeadInfos = request.getHeaderNames();// 获取所有的请求头
        out.write("获取到的客户端所有的请求头信息如下：");
        out.write("<hr/>");
        while (reqHeadInfos.hasMoreElements()) {
            String headName = (String) reqHeadInfos.nextElement();
            // 根据请求头的名字获取对应的请求头的值
            String headValue = request.getHeader(headName);
            out.write(headName + ":" + headValue);
            out.write("<br/>");
        }
        out.write("<br/>");
        out.write("获取到的客户端Accept-Encoding请求头的值：");
        out.write("<hr/>");
        String value = request.getHeader("Accept-Encoding");// 获取Accept-Encoding请求头对应的值
        out.write(value);
        Enumeration<String> e = request.getHeaders("Accept-Encoding");
        while (e.hasMoreElements()) {
            String string = (String) e.nextElement();
            System.out.println(string);
        }
    }

    public void doGet1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestUrl = request.getRequestURL().toString();// 得到请求的URL地址
        String requestUri = request.getRequestURI();// 得到请求的资源
        String queryString = request.getQueryString();// 得到请求的URL地址中附带的参数
        String remoteAddr = request.getRemoteAddr();// 得到来访者的IP地址
        int remotePort = request.getRemotePort(); // 得到来访者的端口
        String method = request.getMethod();// 得到请求URL地址时使用的http方式
        String localAddr = request.getLocalAddr();// 获取WEB服务器的IP地址
        String localName = request.getLocalName();// 获取WEB服务器的主机名
        response.setCharacterEncoding("UTF-8");// 设置将字符以"UTF-8"编码输出到客户端浏览器
        // 通过设置响应头控制浏览器以UTF-8的编码显示数据，如果不加这句话，那么浏览器显示的将是乱码
        response.setHeader("content-type", "text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write("获取到的客户机信息如下：");
        out.write("<hr/>");
        out.write("请求的URL地址：" + requestUrl + "<br/>");
        out.write("请求的资源：" + requestUri + "<br/>");
        out.write("请求的URL地址中附带的参数：" + queryString + "<br/>");
        out.write("来访者的IP地址：" + remoteAddr + "<br/>");
        out.write("使用的端口号：" + remotePort + "<br/>");
        out.write("使用的http方式：" + method + "<br/>");
        out.write("localAddr：" + localAddr + "<br/>");
        out.write("localName：" + localName + "<br/>");
        out.close();
    }


}
